package com.moxiao.yfjh.module.blind.controller.app.controller.message;

import com.moxiao.yfjh.framework.common.pojo.CommonResult;
import com.moxiao.yfjh.framework.common.util.jpush.JgPushSendUrl;
import com.moxiao.yfjh.framework.common.util.jpush.JgPushSendVo;
import com.moxiao.yfjh.module.blind.common.JgPushSend;
import com.moxiao.yfjh.module.blind.controller.admin.memberMessage.vo.MemberMessageCreateReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.sessionRecord.vo.SessionRecordExportReqVO;
import com.moxiao.yfjh.module.blind.dal.dataobject.sessionRecord.SessionRecordDO;
import com.moxiao.yfjh.module.blind.service.memberMessage.MemberMessageService;
import com.moxiao.yfjh.module.blind.service.sessionRecord.SessionRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.moxiao.yfjh.framework.common.pojo.CommonResult.success;

/**
 * @version 1.0
 * @Author zhk
 * @Date 2024/2/1 18:54
 **/

@Tag(name = "App - 相亲聊天记录")
@RestController
@RequestMapping("/blind/member-message")
@Validated
public class AppMemberMessageController {
    @Resource
    private MemberMessageService memberMessageService;
    @Resource
    private SessionRecordService sessionRecordService;
    @Resource
    private JgPushSend jgPushSend;


    @PostMapping("/createMemberMessage")
    @Operation(summary = "创建相亲聊天记录")
    public CommonResult<Integer> createMemberMessage(@RequestBody MemberMessageCreateReqVO createReqVO) {
        String recordId = null;
        SessionRecordExportReqVO exportReqVO = new SessionRecordExportReqVO();
        exportReqVO.setFounder(String.valueOf(createReqVO.getSendMemId()));
        exportReqVO.setParticipants(String.valueOf(createReqVO.getReciveMemId()));
        List<SessionRecordDO> recordList = sessionRecordService.getRecordList(exportReqVO);
        if (CollectionUtils.isNotEmpty(recordList)) {
            recordId = recordList.get(0).getRecordId();
        }
        if (StringUtils.isNotEmpty(createReqVO.getMessageContent())) {
            // 1.生成极光推送
            JgPushSendVo jgPushSendVo = new JgPushSendVo();
            List<String> alias = new ArrayList<>();
            // 1.1消息接收人 推送消息
            alias.add(createReqVO.getReciveMemId().toString());
            jgPushSendVo.setContent(jgPushSend.getStr(createReqVO.getMessageContent()));
            jgPushSendVo.setNotificationTitle("您收到了一个聊天消息");
            jgPushSendVo.setMsgTitle("您收到了一个聊天消息");
            jgPushSendVo.setAlias(alias);
            jgPushSendVo.setUrl(createReqVO.getSendMemId().toString());
            JgPushSendUrl sendUrl = new JgPushSendUrl();
            //sendUrl.setUrl(getStr("/pages/activity/index?id=1001"));
            // 1.2返回 发送人id
            sendUrl.setUrl(createReqVO.getSendMemId().toString());
            sendUrl.setType(2);
            jgPushSendVo.setSendUrl(sendUrl);
            // 2.发送只安卓端
            jgPushSend.sendToAllAndroid(jgPushSendVo);
        }
        if (StringUtils.isNotEmpty(recordId)) {
            createReqVO.setRecordId(recordId);
            return success(memberMessageService.createMemberMessage(createReqVO));

        }
        return success(1);
    }


}
